import { ResponseModel, ResponseStructure } from '@/model/responseModel';
import { RequestServiceProxy } from '@/utils/request';
import { transformTree } from '@/utils/treeMethod';
import { TreeDataNode } from 'antd';
import {
  createRoleRequest,
  createUserRequest,
  dataResponse,
  deleteRequest,
  detailRequest,
  permissionResponse,
  roleDetailResponse,
  roleRequestModel,
  userQueryResponse,
  userResponse,
} from './data';

const _request = new RequestServiceProxy();

// 获取角色列表
export async function getRoleAllList(param?: string):Promise<ResponseStructure<ResponseModel<dataResponse>>> {
  return await _request.post('/api/system/role/list', param);
}

// 获取角色分页列表
export async function getRoleList(
  param?: roleRequestModel,
): Promise<ResponseStructure<ResponseModel<dataResponse>>> {
  return await _request.post('/api/system/role/page', param);
}

//角色查询
export async function queryRole(
  param: detailRequest,
): Promise<ResponseStructure<roleDetailResponse>> {
  return await _request.post<ResponseStructure<roleDetailResponse>>('/api/system/role/query', param);
}

//新增角色
export async function createRole(param: createRoleRequest): Promise<ResponseStructure<void>> {
  return await _request.post<ResponseStructure<void>>('/api/system/role/create', param);
}

//修改角色
export async function updateRole(param: createRoleRequest): Promise<ResponseStructure<void>> {
  return await _request.post<ResponseStructure<void>>('/api/system/role/update', param);
}

//删除角色
export async function deleteRole(param: deleteRequest): Promise<ResponseStructure<void>> {
  return await _request.delete<ResponseStructure<void>>('/api/system/role/delete', param);
}

//权限列表
export async function getPermissionList(): Promise<TreeDataNode[]> {
  let tree: TreeDataNode[] = [];
  const data =
    await _request.post<ResponseStructure<permissionResponse[]>>('/api/system/permission/list');
  if (data.success && data.result) {
    tree = transformTree(data.result);
  }
  return tree;
}

// 获取用户分页列表
export async function getUserList(
  param?: roleRequestModel,
): Promise<ResponseStructure<ResponseModel<userResponse>>> {
  return await _request.post<ResponseStructure<ResponseModel<userResponse>>>('/api/system/user/page', param);
}

//用户查询
export async function queryUser(
  param: detailRequest,
): Promise<ResponseStructure<userQueryResponse>> {
  return await _request.post<ResponseStructure<userQueryResponse>>('/api/system/user/query', param);
}

//新增用户
export async function createUser(param: createUserRequest): Promise<ResponseStructure<void>> {
  return await _request.post<ResponseStructure<void>>('/system/user/create', param);
}

//修改用户
export async function updateUser(param: createUserRequest): Promise<ResponseStructure<void>> {
  return await _request.post<ResponseStructure<void>>('/system/user/update', param);
}

//删除用户
export async function deleteUser(param: deleteRequest): Promise<ResponseStructure<void>> {
  return await _request.post<ResponseStructure<void>>('/system/user/delete', param);
}
